package com.duoec.magichour.core.web.freemarker;

import com.duoec.magichour.core.web.freemarker.model.BaseTemplateModel;
import com.duoec.magichour.core.web.freemarker.model.SortedVar;
import freemarker.core.Environment;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;

/**
 * 本类在XFreemarkerView解析时被创建,并放到env.variables,在整个请求的生命过程中有效
 * 在组件调用时注入组件
 * Created by ycoe on 16/5/18.
 */
public class FreemarkerResourceVars implements TemplateModel {
    private static final Logger logger = LoggerFactory.getLogger(FreemarkerResourceVars.class);

    /**
     * 视图目录,在第一次渲染视图时会被初始化(com.duoec.cms.freemarker.XFreeMarkerView)
     */
    public static File VIEW_DIR;

    /**
     * 变量前缀,为了不与其它变量冲突
     */
    public static final String PREFIX = "mh_x_prefix_";

    /**
     * CSS变量名
     */
    public static final String CSS_VAR_NAME = PREFIX + "css";

    /**
     * JS变量名
     */
    public static final String JS_VAR_NAME = PREFIX + "js";

    private Environment env;

    private String templateName;

    public FreemarkerResourceVars(Environment env, String templateName) {
        this.env = env;
        this.templateName = templateName;
    }

    /**
     * 获取环境变量
     *
     * @param varName
     * @return
     */
    public Object getVar(String varName) throws PortletException {
        try {
            return env.getVariable(varName);
        } catch (TemplateModelException e) {
            logger.error("env.getVariable(\"" + varName + "\")错误", e);
            throw new PortletException(env, e);
        }
    }

    /**
     * 将CSS文件添加进变量
     *
     * @param cssFile
     * @param order
     */
    public void addCss(File cssFile, Integer order) throws PortletException {
        SortedVar resource = getCssSortedVar();
        resource.add(cssFile, order);
    }

    /**
     * 将一段CSS代码添加进变量
     *
     * @param cssCode
     * @param order
     * @param modifyTime 用于判断是否需要更新
     */
    public void addCss(String cssCode, Integer order, long modifyTime) throws PortletException {
        SortedVar resource = getCssSortedVar();
        resource.add(cssCode, order, modifyTime);
    }

    /**
     * 将JS文件添加进变量
     *
     * @param jsFile
     * @param order
     */
    public void addJs(File jsFile, Integer order) throws PortletException {
        SortedVar resource = getJsSortedVar();
        resource.add(jsFile, order);
    }

    /**
     * 将一段JS代码添加进变量
     *
     * @param jsCode
     * @param order
     * @param modifyTime 用于判断是否需要更新
     */
    public void addJs(String jsCode, Integer order, long modifyTime) throws PortletException {
        SortedVar resource = getJsSortedVar();
        resource.add(jsCode, order, modifyTime);
    }

    public SortedVar getCssSortedVar() throws PortletException {
        SortedVar resource = (SortedVar) getVar(CSS_VAR_NAME);
        if (resource == null) {
            resource = new SortedVar();
            setVariable(CSS_VAR_NAME, resource);
        }
        return resource;
    }

    public SortedVar getJsSortedVar() throws PortletException {
        SortedVar resource = (SortedVar) getVar(JS_VAR_NAME);
        if (resource == null) {
            resource = new SortedVar();
            setVariable(JS_VAR_NAME, resource);
        }
        return resource;
    }

    /**
     * 设置环境变量
     *
     * @param name
     * @param model
     */
    public void setVariable(String name, BaseTemplateModel model) {
        this.env.setVariable(name, model);
    }

    public String getTemplateName() {
        return templateName;
    }
}
